/**********************************************************
*                                                         *
*                  彩屏驱动程序                             *
*                                                         *
**********************************************************/
#ifdef SDCC
#include <at89x52.h>
#else
#include <reg52.h>
#endif
#include "utils/msclibs.h"
#include "Lcd.h"
#include "char.h"


uint colors[]=
{
  0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff
};

/**********************************************************

 延时子函数

**********************************************************/
void delay_ms(uint ms)
{
  unsigned int k;

  while (ms--)
  {
    for (k = 0; k < 320; k++)
      ;
  }
}

/**********************************************************

 写命令与数据子函数

**********************************************************/
void LCD_Write(uchar type, uint value)  
{
    LCD_CS = 0;
    LCD_RS  = type;         // 0: command    1: data
    
    LCD_WR  = 0;
/// DATA    = (uchar)value;
    DATA    = (uchar)(value>>8) ;
    LCD_WR  = 1;
    LCD_WR  = 0;
//  DATA    = (uchar)(value>>8) ;
    DATA    = (uchar)value;
    LCD_WR  = 1;
    LCD_CS = 1;
}

/**********************************************************

 写8位数据子函数

**********************************************************/
/*void LCD_Write_Data8(uchar value)    // color data
{
    LCD_CS = 0;
    LCD_RS  = 1;
    LCD_WR  = 0;
    DATA    = value;
    LCD_WR  = 1;
    LCD_CS = 1;
}*/


/**********************************************************

 写16位数据子函数

**********************************************************/
void LCD_Wirte_Data16(uint value)    // color data
{
    LCD_CS = 0;
    LCD_RS  = 1;
    LCD_WR  = 0;
    //DATA  = (uchar)value;
    DATA    = (uchar)(value>>8) ;
    LCD_WR  = 1;
    LCD_WR  = 0;
    //DATA  = (uchar)(value>>8) ;
    DATA    = (uchar)value;
    LCD_WR  = 1;
    LCD_CS = 1;
}

/*********************************************************

 写寄存器子函数

**********************************************************/
void Reg_Write(uint reg,uint value) 
{
   LCD_Write(TYPE_LCD_COMMAND,reg);
   LCD_Wirte_Data16(value);
}


/**********************************************************

设置显示窗口子函数

**********************************************************/
void Address_set(uint xStart, uint xEnd, uint yStart, uint yEnd) 
{

    Reg_Write(0x200, xStart); 
    Reg_Write(0x201, yStart); 

    Reg_Write(0x0210, xStart);
    Reg_Write(0x0212,yStart);
    
    Reg_Write(0x211,xEnd);
    Reg_Write(0x213,yEnd);



    LCD_Write(TYPE_LCD_COMMAND,0x0202);     // 0x22
}


/**********************************************************/
void LCD_init(void)
{   
       LCD_RST=1;
       delay_ms(5); 
        LCD_RST=0;
        delay_ms(5);
        LCD_RST=1;
        delay_ms(5);
    
        LCD_CS =0;  //打开片选使能

    LCD_Wirte_Data16(0x0000);delay_ms(5);
    LCD_Wirte_Data16(0x0000);delay_ms(5);   
    delay_ms(10);delay_ms(5); //DelayX1mS(1); 
    LCD_Wirte_Data16(0x0000);delay_ms(5);
    LCD_Wirte_Data16(0x0000);delay_ms(5);   
    LCD_Wirte_Data16(0x0000);delay_ms(5);   
    LCD_Wirte_Data16(0x0000);delay_ms(5);   
    delay_ms(20);//DelayX1mS(10);//necessary    



    Reg_Write(0x000,0x0000);delay_ms(5);
    Reg_Write(0x000,0x0000);delay_ms(5);
    Reg_Write(0x000,0x0000);delay_ms(5);
    Reg_Write(0x000,0x0000);delay_ms(5);
    delay_ms(10);

    Reg_Write(0x008,0x0808);delay_ms(5);
    Reg_Write(0x010,0x0010);delay_ms(5);
    Reg_Write(0x400,0x6200);delay_ms(5);

    Reg_Write(0x300,0x0c06);delay_ms(5);//GAMMA
    Reg_Write(0x301,0x9d0f);delay_ms(5);
    Reg_Write(0x302,0x0b05);delay_ms(5);
    Reg_Write(0x303,0x1217);delay_ms(5);
    Reg_Write(0x304,0x3333);delay_ms(5);
    Reg_Write(0x305,0x1712);delay_ms(5);
    Reg_Write(0x306,0x950b);delay_ms(5);    
    Reg_Write(0x307,0x0f0d);delay_ms(5);
    Reg_Write(0x308,0x060c);delay_ms(5); 
    Reg_Write(0x309,0x0000);delay_ms(5);

    Reg_Write(0x011,0x0202);delay_ms(5);
    Reg_Write(0x012,0x0101);delay_ms(5);
    Reg_Write(0x013,0x0001);delay_ms(5);

    Reg_Write(0x007,0x0001);delay_ms(5);
    Reg_Write(0x100,0x0730);delay_ms(5);//BT,AP 0x0330
    Reg_Write(0x101,0x0237);delay_ms(5);//DC0,DC1,VC
    Reg_Write(0x103,0x2b00);delay_ms(5);//VDV   //0x0f00
    Reg_Write(0x280,0x4000);delay_ms(5);//VCM
    Reg_Write(0x102,0x81b0);delay_ms(5);//VRH,VCMR,PSON,PON
    delay_ms(100);

    Reg_Write(0x001,0x0100);delay_ms(5);
    Reg_Write(0x002,0x0100);delay_ms(5);
    Reg_Write(0x003,0x1030);delay_ms(5);
    Reg_Write(0x009,0x0001);delay_ms(5);
    
    Reg_Write(0x0C,0x0000); delay_ms(5);//MCU interface 
//   Reg_Write(0x0C,0x0110);    //RGB interface 18bit
//   Reg_Write(0x0C,0x0111);    //RGB interface 16bit
//   Reg_Write(0x0C,0x0020);    //VSYNC interface

    Reg_Write(0x090,0x8000);delay_ms(5);
    Reg_Write(0x00f,0x0000); delay_ms(5);

    Reg_Write(0x210,0x0000); delay_ms(5);
    Reg_Write(0x211,0x00ef); delay_ms(5);
    Reg_Write(0x212,0x0000); delay_ms(5);
    Reg_Write(0x213,0x018f); delay_ms(5);

    Reg_Write(0x500,0x0000);delay_ms(5);
    Reg_Write(0x501,0x0000);delay_ms(5);
    Reg_Write(0x502,0x005f);delay_ms(5);
    Reg_Write(0x401,0x0001);delay_ms(5);
    Reg_Write(0x404,0x0000);delay_ms(5);
    delay_ms(100);  
    Reg_Write(0x007,0x0100);delay_ms(5);
    delay_ms(100);
    Reg_Write(0x200,0x0000);delay_ms(5);
    Reg_Write(0x201,0x0000);delay_ms(5);

    //WriteCom_Xbit(0x202);
    LCD_Write(TYPE_LCD_COMMAND,0x0202);

}


/**********************************************************

 清屏子函数

**********************************************************/
void  LCD_clear(uchar n)
{
  unsigned int com,seg; 

    Address_set(0,239, 0,399);
    for(com=0;com<400;com++)
    {
        for(seg=0;seg<240;seg++)
        {
             // WriteData_Xbit(color);
             LCD_Wirte_Data16(colors[n]);            
        }
    }

}


/**********************************************************

  字符显示子函数

**********************************************************/
void LCD_ShowChar(uint x,uint y,uint For_color,uint Bk_color, char ch)
{       
   uchar temp;
   uchar pos,t;
   uint  CHAR_W,CHAR_H;
   
   CHAR_W = 8;         //8*16
   CHAR_H = 16;

   if(x>(LCD_SIZE_X-CHAR_W)||y>(LCD_SIZE_Y-CHAR_H))
   return;
   
   Address_set(x, x+CHAR_W-1, y,y+CHAR_H-1);
   ch = ch-32;                       //按照ASCII编码顺序的到相应字母的编码

   for(pos=0;pos<CHAR_H;pos++)  
   {
      temp= Font8x16[ch][pos];
      
      for(t=0;t<CHAR_W;t++)
      {                 
        if(temp&0x80)
        LCD_Wirte_Data16(For_color);  //textcolor
        else 
        LCD_Wirte_Data16(Bk_color);  //backcolor
        temp<<=1; 
      }
      
   }
}  

/**********************************************************
 显示字符串子函数

 x,y:起点坐标  
 *p:字符串起始地址
                                           
**********************************************************/
void LCD_ShowString( uint x,uint y,uint For_color,uint Bk_color,char *p)
{         
   while(*p!='\0')
   {       
     LCD_ShowChar(x,y,For_color,Bk_color,*p);
     x+=8;
     p++;
   }
}

void LCD_ShowInt( uint x,uint y,uint For_color,uint Bk_color,int value)
{   
    int bitv=0;
    char chars[]={0,0,0,0,0,0,0,0,0,0};
    char* pos=chars;
    while(value>0){
        bitv = value%10;
        *pos = bitv;
        pos++;
        value=value/10;
    };
    while(pos>chars){
        pos--;
        LCD_ShowChar(x,y,For_color,Bk_color,*pos|0x30);
        x+=8;
       
    } 
}

/*********************************************************/